package tools;

import java.sql.*;

public class Buku {

    private int kode_buku;
    private String judul;
    private String pengarang;
    private String penerbit;
    private String cover;
    private String pdf;
    private String keyword;
    private int jumlah_baca = 0;
    private String koneksi = "jdbc:Oracle:thin:@localhost:1521:XE";
    private String username = "system";
    private String password = "hr";
    private Connection con = null;
    private Statement stmt = null;
    private ResultSet rs = null;
    private DatabaseMetaData dmd;
    private ResultSetMetaData rsmd;

    /*
     * method dibawah digunakan untuk menampilkan data yang ada di data base dalam bentuk tabel.
     */
    public String aksesDatabase() {
        String dbHTML = "";

        if (koneksi == null) {
            password = "";
            return dbHTML;
        }
        if (koneksi.equals("")) {
            password = "";
            dbHTML = "Koneksi error. Mungkin nama tabel kosong.";
            return dbHTML;
        }
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
//            con = DriverManager.getConnection(koneksi,
//                    "system", "hr");
            con = DriverManager.getConnection("jdbc:Oracle:thin:@172.23.9.185:1521:orcl",
                    "MHS105314089", "MHS105314089");
            stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM BUKU ORDER BY KODE_BUKU");
            dmd = con.getMetaData();
            rsmd = rs.getMetaData();
            int nColumn = rsmd.getColumnCount();
            dbHTML += "<table border=\"1\" cellpadding=\"4\"><tr>";
            for (int i = 1; i <= nColumn; i++) {
                dbHTML += "<th>" + rsmd.getColumnName(i) + "</th>";
            }
            dbHTML += "</tr>";
            while (rs.next()) {
                dbHTML += "<tr>";
                for (int i = 1; i <= nColumn; i++) {
                    dbHTML += "<td>" + rs.getString(i) + "</td>";
                }
                dbHTML += "</tr>";
            }
            dbHTML += "</table>";
        } catch (ClassNotFoundException e) {
            dbHTML += "<p>Error loading driver: " + e.getMessage() + "</p>";
        } catch (SQLException e) {
            dbHTML += "<p>Error SQL: " + e.getMessage() + "</p>";
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException sqle) {
            }
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException sqle) {
            }
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException sqle) {
            }
        }
        password = "";
        return dbHTML;
    }

    /*
     * method dibawah digunakan untuk membuat autogenerate pada kode buku.
     */
    public int getKode_buku() throws SQLException {
        int id = 0;
        Statement st = null;
        ResultSet res = null;
        try {
            String query = "SELECT MAX(KODE_BUKU) FROM BUKU";
            st = KoneksiDataBase.getConnection().createStatement();
            res = st.executeQuery(query);

            while (res.next()) {
                id = res.getInt(1) + 1;
            }
        } catch (SQLException sqle) {
            sqle.printStackTrace();
            System.out.println("gagal");
        }
        return id;
    }

    public void setKode_buku(int kode_buku) {
        this.kode_buku = kode_buku;
    }

    public int getKode() {
        return kode_buku;
    }

    public String getPengarang() {
        return pengarang;
    }

    public void setPengarang(String pengarang) {
        this.pengarang = pengarang;
    }

    public String getPenerbit() {
        return penerbit;
    }

    public void setPenerbit(String penerbit) {
        this.penerbit = penerbit;
    }

    public String getCover() {
        return cover;
    }

    public void setCover(String cover) {
        this.cover = cover;
    }

    public String getPdf() {
        return pdf;
    }

    public void setPdf(String pdf) {
        this.pdf = pdf;
    }

    public String getJudul() {
        return judul;
    }

    public void setJudul(String judul) {
        this.judul = judul;
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public int getJumlah_baca() {
        return jumlah_baca;
    }

    public void setJumlah_baca(int jumlah_baca) {
        this.jumlah_baca = jumlah_baca;
    }
}
